﻿using AutoMapper;
using B.S.AuthCenter.Api.Write.Application.Command.Roles;
using B.S.AuthCenter.Domain;
using B.S.AuthCenter.ErrorCount;
using B.S.AuthCenter.InfrasIntructure.Interface;
using MediatR;

namespace B.S.AuthCenter.Api.Write.Application.Handler.Roles
{
    public class DelRoleHandler : IRequestHandler<DelRoleCommand, ApiResult<int>>
    {
        IBaseRepository<RoleModel> roleRepo;
       

        public DelRoleHandler(IBaseRepository<RoleModel> roleRepo)
        {
            this.roleRepo = roleRepo;
           
        }

        public Task<ApiResult<int>> Handle(DelRoleCommand request, CancellationToken cancellationToken)
        {
            ApiResult<int> res = new ApiResult<int>();
            var role = roleRepo.GetAll().Where(c => c.Id == request.Id).FirstOrDefault();
            if (role==null)
            {
                res.Msg = "您要删除的内容不存在";
                res.Code=ApiEnum.Fail;
                return Task.FromResult(res);
            }
            role.IsDeleted = true;
            int a=roleRepo.Update(role).Result;
            if (a > 0)
            {
                res.Msg = "角色名称信息删除成功";
                res.Code = ApiEnum.Success;
                res.Datas = a;
                return Task.FromResult(res);
            }
            else
            {
                res.Msg = "角色名称信息删除失败";
                res.Code = ApiEnum.Success;
                res.Datas = a;
                return Task.FromResult(res);
            }
        }
    }
}
